package com.yantong.utils;

import org.apache.commons.lang3.StringUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;

public class Utils {

    /**
     * 获取参数拼接
     */
    public static String getParamStr(Map<String, Object> paramMap) {
        if(paramMap == null){
            return null;
        }
        SortedMap<String, Object> map = new TreeMap<>(paramMap);
        StringBuilder stringBuffer = new StringBuilder();
        for (Map.Entry<String, Object> m : map.entrySet()) {
            Object value = m.getValue();
            if (value != null && !isBlank(String.valueOf(value))) {
                stringBuffer.append(m.getKey()).append("=").append(value).append("&");
            }
        }
        stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());
        return stringBuffer.toString();
    }
    /**
     * 实体转换成MAP
     *
     * @param object 要转换的对象
     * @return 根据对象属性返回map
     */
    public static Map<String, Object> objectToMap(Object object) {
        Map<String, Object> map = new HashMap<>();
        try {
            if (object == null) {
                return null;
            }
            BeanInfo beanInfo;
            beanInfo = Introspector.getBeanInfo(object.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();
                if (key.compareToIgnoreCase("class") == 0) {
                    continue;
                }
                Method getter = property.getReadMethod();
                Object value = getter != null ? getter.invoke(object) : null;
                map.put(key, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }


    /**
     * 判断字符串是否为空
     * @param str   要判断的字符串
     * @return      true为空，false不为空
     */
    public static boolean isBlank(String str) {
        int strLen;
        if(str != null && (strLen = str.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if(!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }
            return true;
        } else {
            return true;
        }
    }

//    /**
//     * http post
//     *
//     * @param url   发送地址
//     * @param paramMap  参数列表
//     * @return  返回的字符串对象
//     */
//    public static String postSubmit(String url, Map<String, Object> paramMap) {
//        String result;
//        HttpResponse httpResponse;
//        HttpClient client = null;
//        try {
//            client = new SSLClient();
//           // client = new DefaultHttpClient();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        //post method
//        HttpPost httppost = new HttpPost(url);
//        try {
//            //参数添加
//            List<NameValuePair> params = new ArrayList<>();
//            for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
//                params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
//            }
//            httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//
//            // 发送请求
//            httpResponse = client.execute(httppost);
//            result = EntityUtils.toString(httpResponse.getEntity());
//        } catch (Exception ex) {
//            throw new RuntimeException(ex);
//        } finally {
//            httppost.releaseConnection();
//        }
//        return result;
//    }

    //只校验正数 0-90.000000 0-180.000000 范围内
    /**
     * 经纬度校验
     * 经度longitude: (?:[0-9]|[1-9][0-9]|1[0-7][0-9]|180)\\.([0-9]{6})
     * 纬度latitude：  (?:[0-9]|[1-8][0-9]|90)\\.([0-9]{6})
     * @return
     */
    public static boolean checkItude(String longitude,String latitude){
        if(StringUtils.isEmpty(longitude) || StringUtils.isEmpty(latitude)){
            return false;
        }
        String reglo = "((?:[0-9]|[1-9][0-9]|1[0-7][0-9])\\.([0-9]{0,16}))|((?:180)\\.([0]{0,16}))";
        String regla = "((?:[0-9]|[1-8][0-9])\\.([0-9]{0,16}))|((?:90)\\.([0]{0,16}))";
        longitude = longitude.trim();
        latitude = latitude.trim();
        return longitude.matches(reglo)==true?latitude.matches(regla):false;
    }

    /**
     * 随机生成n位不重复字符串(字母小写+数字组合)
     * @return
     */
    public static String getNumStringNotRe(int n){
        char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p','q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','0','1','2','3','4','5','6','7','8','9'};
        boolean[] flags = new boolean[letters.length];
        char[] chs = new char[n];
        for (int i = 0; i < chs.length; i++) {
            int index;
            do {
                index = (int) (Math.random() * (letters.length));
            } while (flags[index]);// 判断生成的字符是否重复
            chs[i] = letters[index];
            flags[index] = true;
        }
        return String.valueOf(chs);
    }

    /**
     * 随机生成n位可重复字符串(字母小写+数字组合)
     * @return
     */
    public static String getNumString(int n){
        char[] letters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p','q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','0','1','2','3','4','5','6','7','8','9'};
        char[] chs = new char[n];
        for (int i = 0; i < chs.length; i++) {
            int index = (int) (Math.random() * (letters.length));
            chs[i] = letters[index];
        }
        return String.valueOf(chs);
    }

}
